package com.dishdex.menuhub.module.utils;

import com.dishdex.menuhub.module.module.user.entity.UserSign;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.module.utils
 * Description:
 *
 * @Author:
 * @Create 2025/10/29 20:48
 * @Version 1.0
 */

public class SignUtils {
    public final static int EXPIRATION_TIME = 1209600;

    public static String makeSign(Long userId) {
        UserSign userSign = new UserSign();
        userSign.setUserId(userId);
        userSign.setTime(BaseUtils.currentSeconds() + EXPIRATION_TIME);
        try {
            return PersonJwt.packObject(userSign);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Long parseSign(String sign) {
        UserSign userSign;
        try {
            userSign = PersonJwt.unpackString(sign, UserSign.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        int time = userSign.getTime();
        int currentTime = BaseUtils.currentSeconds();
        if (currentTime > time) {
            return null;
        }
        return userSign.getUserId();
    }
}
